﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class hotpro : System.Web.UI.Page
    {
        // Fields

        public string content = "";
        public string sPriceName = "";
        public string sLoginMessage = "&nbsp;温馨提示：请登录并充值后，选取您要兑换的商品！</br>";
        private static readonly Regex RX = new Regex("&amp;page=d+", RegexOptions.Compiled);
        readonly DALShoppro theShoppro = new DALShoppro();
        public StringBuilder sProductHtml = new StringBuilder();

        string _builderOutPageHeaderHtml = string.Empty;


        public string ArrowDirectionSale
        {
            get
            {
                if (this.Context.Session["ArrowDirectionSale"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionSale"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionSale"] = value;
            }
        }

        public string ArrowDirectionPrice
        {
            get
            {
                if (this.Context.Session["ArrowDirectionPrice"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionPrice"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionPrice"] = value;
            }
        }

         string GetBuilderOutPageHeaderHtml()
        {
        
                string input = this.Context.Request.Url.Query.Replace("?", "&amp;");
                input = RX.Replace(input, string.Empty);
                input = input.Replace("&amp;order=sale", string.Empty);
                input = input.Replace("&amp;order=price", string.Empty);
                input = input.Replace("&amp;order=time", string.Empty);
                string sLiClass1 = string.Empty;
                string sLiClass2 = string.Empty;
                string sLiClass3 = string.Empty;
                string sArrow1 = string.Empty;
                string sArrow2 = string.Empty;
                if (string.IsNullOrEmpty(this.Context.Request["order"]) || this.Context.Request["order"] == "sale")
                {
                    if (!string.IsNullOrEmpty(this.Context.Request["order"]))
                    {
                        if (ArrowDirectionSale == "&darr;")
                        {
                            ArrowDirectionSale = "&uarr;";
                        }
                        else
                        {
                            ArrowDirectionSale = "&darr;";
                        }
                    }
                    sArrow1 = ArrowDirectionSale;
                    sLiClass1 = "class='currentPage'";
                    sArrow2 = string.Empty;
                }
                else if (this.Context.Request["order"] == "price")
                {
                    if (ArrowDirectionPrice == "&darr;")
                    {
                        ArrowDirectionPrice = "&uarr;";
                    }
                    else
                    {
                        ArrowDirectionPrice = "&darr;";
                    }
                    sArrow2 = ArrowDirectionPrice;
                    sArrow1 = string.Empty;
                    sLiClass2 = "class='currentPage'";
                }
                else if (this.Context.Request["order"] == "time")
                {
                    sLiClass3 = "class='currentPage'";
                }
                StringBuilder sbPageHeader = new StringBuilder();
                sbPageHeader.Append("  <div style=\" width:100%; text-align:left;  height:19px; padding-left:5px; float:left\">");
                sbPageHeader.Append("  <div style='float:left;'>排序</div>");
                sbPageHeader.Append("   <div id=\"orderMenu\">");
                sbPageHeader.Append("  <ul>");
                sbPageHeader.Append(string.Format("  <li {1}><a href=?order=sale{0}>{4}销  量</a></li><li {2}><a href=?order=price{0}>{5}价  格</a></li><li {3}><a href=?order=time{0}>上价时间</a></li>", input, sLiClass1, sLiClass2, sLiClass3, sArrow1, sArrow2));
                sbPageHeader.Append("  </ul>");
                sbPageHeader.Append("  </div>");

                sbPageHeader.Append(" </div>");

                return sbPageHeader.ToString();
                //System.Web.UI.HtmlControls.HtmlGenericControl divHtml = new System.Web.UI.HtmlControls.HtmlGenericControl();
                //divHtml.InnerHtml = sbPageHeader.ToString();
         
        }

        // Methods
        private void BindDataList(int iPageType)
        {
            DataTable table = SQLAccess.GreatDs("select shuxiname from DB_Shuxintype where shuxinid=" + re.replace(base.Request.QueryString["sid"]) + "").Tables[0];
            if (table.Rows.Count > 0)
            {
                this.content = table.Rows[0][0].ToString();
            }

            int iCardtypeId = -1;
            


            DataSet ds = theShoppro.GetDataListPage(base.Request.QueryString["sid"]
                                                 , Request.QueryString["typeid"]
                                                 , Request.QueryString["order"]
                                                 , ArrowDirectionSale
                                                 , ArrowDirectionPrice
                                                 , this.AspNetPager1.StartRecordIndex - 1
                                                 , this.AspNetPager1.StartRecordIndex * this.AspNetPager1.PageSize
                                                 , iCardtypeId
                                                 , string.Empty
                                                 , Request.QueryString["key"]
                                                 );


            if (ds != null)
            {
                if (iPageType == 1)
                {
                    AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
                    AspNetPager2.RecordCount = AspNetPager1.RecordCount;
                }
                else if (iPageType == 2)
                {
                    AspNetPager2.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
                    AspNetPager1.RecordCount = AspNetPager2.RecordCount;
                }

                BindProductList(ds.Tables[0]);
            }


            this.tdOrder.InnerHtml = _builderOutPageHeaderHtml;
        }

        void BindProductList(DataTable dtProduct)
        {
            sProductHtml = new StringBuilder();
            if (dtProduct == null || dtProduct.Rows.Count <= 0) return;

            sProductHtml.Append("<div class=\"info_list\"><ul>");
            string sProName = string.Empty;
            foreach (DataRow dr in dtProduct.Rows)
            {
                sProName = dr["proName"].ToString().Length > 12 ? dr["proName"].ToString().Substring(0, 12) + ".." : dr["proName"].ToString();
                sProductHtml.AppendFormat("<li>"
                                + " <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"130\" height=\"130\" alt=\"\" src=\"{2}\" /></a>"
                                + " <p><a href=\"{0}\" target=\"_blank\" title=\"{7}\">{3}</a><em>库存：{4}</em><em>零售价：￥{5}</em></p>"
                                + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品编号:{6}</span> <span><em>库存：{4}</em></span><span><em>兑换价￥{5}</em></span></p></div>"
                                + "</li>"
                                , "products_details.aspx?pid=" + dr["proid"].ToString() + "&sid=" + Request.QueryString["sid"]
                                , dr["proid"].ToString()
                                , "/uploadfile/product/" + dr["propic2"].ToString()
                                , sProName
                                , dr["proku1"].ToString()
                                , String.Format("{0:f}", dr["proprice1"].ToString())
                                , dr["procode"].ToString()
                                , dr["proName"].ToString()
                                );
            }

            sProductHtml.Append("</ul></div>");//<div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
        }




        public void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((base.Request.QueryString["sid"] != null) && ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)))
            {
                Label label = (Label)e.Item.FindControl("price");
                Label label2 = (Label)e.Item.FindControl("Label1");
                Image image = (Image)e.Item.FindControl("Image1");
                if ((label.Text == "0") || (label.Text == "0.00"))
                {
                    label.Text = label2.Text;
                }
                if (image.ImageUrl.ToString() == "")
                {
                    image.ImageUrl = "../uploadfile/banner/" + info.info1("", "shopmopic");
                }
                else
                {
                    image.ImageUrl = "../uploadfile/product/" + image.ImageUrl.ToString();
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            Session["backHref"] = Request.Url.PathAndQuery;
            if (!base.IsPostBack && (base.Request.QueryString["sid"] != null))
            {
                _builderOutPageHeaderHtml = GetBuilderOutPageHeaderHtml();
                sPriceName = base.Request.QueryString["sid"] == "2" ? "兑换价":"零售价";
               this.AspNetPager1.CurrentPageIndex = 1;
               this.AspNetPager2.CurrentPageIndex = 1;
            //    BindDataList(1);
            }
        }

  

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            this.AspNetPager2.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex;
            this.BindDataList(1);

        }

        protected void AspNetPager2_PageChanged(object src, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = this.AspNetPager2.CurrentPageIndex;
            this.BindDataList(2);

        }



    }
}